package com.xinhe.cn.cpbox.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinhe.cn.cpbox.common.Result;
import com.xinhe.cn.cpbox.entity.po.MatchMaker;
import com.xinhe.cn.cpbox.entity.po.SystemConstant;
import com.xinhe.cn.cpbox.entity.po.UserInfo;
import com.xinhe.cn.cpbox.entity.po.UserRecord;
import com.xinhe.cn.cpbox.mapper.MatchMakerMapper;
import com.xinhe.cn.cpbox.mapper.SystemConstantMapper;
import com.xinhe.cn.cpbox.mapper.UserRecordMapper;
import com.xinhe.cn.cpbox.service.MoonService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author HB
 * @date 2021/10/09 11:37
 */
@Slf4j
@Service
public class MoonServiceImpl implements MoonService {
    @Autowired
    MatchMakerMapper matchMakerMapper;
    @Autowired
    SystemConstantMapper systemConstantMapper;
    @Autowired
    UserRecordMapper userRecordMapper;

    /**
     * 客服二维码
     */
    private String serviceQrCode = null;

    @Override
    public Object findMatchMakerInfo(String matchMakerId) {
        MatchMaker matchMaker = matchMakerMapper.selectById(matchMakerId);
        return Result.ok().data("matchMakerInfo", matchMaker);
    }

    @Override
    public Object getServiceQrCode() {
        if (StringUtils.isBlank(serviceQrCode)) {
            SystemConstant code = systemConstantMapper.selectById("serviceQrCode");
            if (!Objects.isNull(code)) {
                serviceQrCode = code.getConstantValue();
            }
        }
        return Result.ok().data("serviceQrCode", serviceQrCode);
    }

    @Override
    public Object changeServiceQrCode(JSONObject jsonObject) {
        String codeUrl = jsonObject.getString("codeUrl");
        if (StringUtils.isBlank(codeUrl)) {
            return Result.paramError();
        }

        serviceQrCode = codeUrl;

        SystemConstant code = systemConstantMapper.selectById("serviceQrCode");
        if(Objects.isNull(code)) {
            code = new SystemConstant();
            code.setName("serviceQrCode");
            code.setConstantValue(codeUrl);
            systemConstantMapper.insert(code);
        } else {
            code.setConstantValue(codeUrl);
            systemConstantMapper.updateById(code);
        }

        return Result.ok();
    }

    @Override
    public Object sendCashOutApply(MatchMaker matchMaker) {
        String needMoney = matchMaker.getNeedMoney();
        String qrCode = matchMaker.getQrCode();
        String realName = matchMaker.getRealName();
        String contactWechat = matchMaker.getContactWechat();

        if (StringUtils.isBlank(needMoney) || StringUtils.isBlank(qrCode) ||
                StringUtils.isBlank(realName) || StringUtils.isBlank(contactWechat)) {
            return Result.paramError();
        }

        MatchMaker allInfo = matchMakerMapper.selectById(matchMaker.getMatchMakerId());
        if (Objects.isNull(allInfo)) {
            return Result.error().message("找不到月老信息");
        }

        allInfo.setNeedMoney(needMoney);
        allInfo.setQrCode(qrCode);
        allInfo.setRealName(realName);
        allInfo.setContactWechat(contactWechat);
        allInfo.setHasApply("1");
        allInfo.setApplyTime(DateUtil.now());

        matchMakerMapper.updateById(allInfo);

        return Result.ok();
    }

    @Override
    public Object getMyChilds(String matchMakerId, int type, int page, int size) {
        if (StringUtils.isBlank(matchMakerId) || page < 0 || size <= 0 || type < 1 || type > 2) {
            return Result.paramError();
        }

        // 查询条件
        QueryWrapper<MatchMaker> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(type == 1, "parent_id", matchMakerId) // 查询下级月老
                .eq(type == 2, "grandparent_id", matchMakerId) // 查询下下级月老
                .orderByDesc("apply_time");

        // 分页
        Page<UserInfo> mpPage = new Page<>(page, size);

        // 查询
        IPage<UserInfo> iPage = matchMakerMapper.selectMakerByParent(mpPage, queryWrapper);

        // 返回总页数和数据
        return Result.ok().data("totalPage", iPage.getPages()).data("matchMakerList", iPage.getRecords());
    }

    @Override
    public Object getUserRecord(String openId, int page, int size) {
        if (StringUtils.isBlank(openId) || page < 0 || size <= 0) {
            return Result.paramError();
        }

        // 查询条件
        QueryWrapper<UserRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("open_id", openId)
                .orderByDesc("create_time");

        // 分页
        Page<UserRecord> mpPage = new Page<>(page, size);

        // 查询
        IPage<UserRecord> iPage = userRecordMapper.selectPage(mpPage, queryWrapper);

        // 返回总页数和数据
        return Result.ok().data("totalPage", iPage.getPages()).data("records", iPage.getRecords());
    }
}
